package com.ssy.lingxi.platform.manage.serviceimpl.mobile;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.manage.entity.content.MemberCategory;
import com.ssy.lingxi.platform.manage.model.response.CategoryResp;
import com.ssy.lingxi.platform.manage.repository.MemberCategoryRepository;
import com.ssy.lingxi.platform.manage.service.mobile.IMemberCategoryMobileService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * mobile - 自营商家 - 分类 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2021/10/25
 */
@Service
public class MemberCategoryMobileServiceImpl implements IMemberCategoryMobileService {

    @Resource
    private MemberCategoryRepository memberCategoryRepository;

    @Override
    public Wrapper<List<CategoryResp>> all(Long memberId, Long roleId) {
        //校验
        if(memberId == null || roleId == null) {
            return Wrapper.fail(ResponseCode.PT_PARAMETER_VERIFICATION_FAILED);
        }

        //获取所有分类
        List<MemberCategory> all = memberCategoryRepository.findAllByMemberIdAndMemberRoleId(memberId, roleId);

        //先拿到所有一级分类再根据父级ID匹配递归封装子级分类
        List<CategoryResp> list = all.stream().filter(a -> a.getParentId().equals(0L)
        ).map(b -> {
            CategoryResp resp = new CategoryResp();
            BeanUtils.copyProperties(b, resp);
            resp.setList(getChildren(b, all));
            return resp;
        }).sorted(Comparator.comparingLong(CategoryResp::getId)).collect(Collectors.toList());

        //获取三级分类（推荐）
        List<MemberCategory> recommendList = all.stream().filter(a ->
                a.getStatus().equals(CommonBooleanEnum.YES.getCode()) && a.getLevel().equals(3)
        ).collect(Collectors.toList());

        //将推荐的第三级分类根据第一级分类ID进行分组筛选
        Map<Long, List<CategoryResp>> map = recommendList.stream().map(a -> {
            CategoryResp resp = new CategoryResp();
            BeanUtils.copyProperties(a, resp);
            return resp;
        }).sorted(Comparator.comparingLong(CategoryResp::getId)).collect(Collectors.groupingBy(CategoryResp::getFirstId));

        //将一级分类按map内的key进行筛选并完成最后封装
        list = list.stream().peek(a -> a.setRecommendList(map.get(a.getId()))).collect(Collectors.toList());

        return Wrapper.success(list);
    }

    /**
     * 分类递归
     * */
    private List<CategoryResp> getChildren(MemberCategory root, List<MemberCategory> all) {
        return all.stream().filter(a -> a.getParentId().equals(root.getId()))
                .map(b -> {
                    CategoryResp resp = new CategoryResp();
                    BeanUtils.copyProperties(b, resp);
                    resp.setList(getChildren(b, all));
                    return resp;
                }).sorted(Comparator.comparingLong(CategoryResp::getId)).collect(Collectors.toList());
    }
}
